\documentclass[border=10pt,tikz]{standalone}
\usepackage{pgfplots,pgfplotstable,tikz}
\usetikzlibrary{intersections,matrix}
\usepackage{verbatim}
\usepackage{moresize}
\usetikzlibrary{calc}
\usepackage{datatool}
\usetikzlibrary{shapes}
\usetikzlibrary{plotmarks}
\usepackage{csvsimple}
\usepackage{siunitx}
\usepackage{mathtools}
\usepackage{amsmath,amssymb}
\usetikzlibrary{shapes.geometric}
\usepackage{booktabs}
\usepackage{xspace} 
\usepackage{etoolbox}
\usepackage{multirow}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.text}
\usetikzlibrary{decorations.pathmorphing}
\pgfplotsset{compat=1.13}
\usepackage{fp}
\usepgfplotslibrary{fillbetween}


\definecolor{green}{RGB}{68,126,35}
\definecolor{blue}{RGB}{19,30,92}
\definecolor{red}{RGB}{219,51,24}

% Defines "sgn" function
\def\sgn{\mathop{\rm sgn}\nolimits} 
% Defines the expectation function
\def\E{\mathop{\mathrm{E}}\nolimits} 

% Defines a function that ignores the zero on the y- or the x-axis, such that the origin
% is only labeled once. Activate in the axis options by adding the key: "ignore zero=x" or ""ignore zero=y."
\pgfplotsset{ignore zero/.style={%
		#1ticklabel={\ifdim\tick pt=0pt \else\pgfmathprintnumber{\tick}\fi}
	}}

	
	
	\begin{document}
		% TAKE INPUTS FROM THE FOLLOWING FILES:
		% "linmte_no_covars_Y_num_oregonnumhh1.csv" --- generated in Stata by the program file: "linmte_no_covars_graph_data.do"
		% "linmte_no_covars_treat_eff.xls" (this file is used only for obtaining the standard errors reported in the figure legend) --- generated in Stata by the program file: "linmte_no_covars_treat_eff.do"
		
		% Directly load the csv file 
		\begingroup\catcode`"=9 % ignore quotation marks in csv file
		\csvreader[head to column names, filter test = \ifnumequal{\thecsvinputline}{2}]{linmte_no_covars_Y_num_oregonnumhh1.csv}{}{
			% Enforce display of decimal points
			\pgfkeys{/pgf/number format/fixed zerofill=true}
			
			% Store Labels
			\newcommand*{\D}{\Dlab\xspace}
			\newcommand*{\Y}{\Ylab\xspace}
			
			% Calculate the midpoint between 0 and pB
			\pgfmathsetmacro\xone{\pB*0.5}
			% Calculate the midpoint between pB and pI
			\pgfmathsetmacro\xtwo{\pB*0.5+\pI*0.5}
			% Calculate the midpoint between pI and 1
			\pgfmathsetmacro\xthree{\pI*0.5+0.5}
			
			\pgfmathsetmacro\LATE{\LATO-\LAUO}
			
			% [Mslope] Calculate the slope of Mammograms, connecting: (\xone, \BTTO) and (\xtwo, \LATO)
			\pgfmathsetmacro\xonextwo{\xtwo - \xone}
			\pgfmathsetmacro\BTTOCM{\LATO-\BTTO}
			\pgfmathsetmacro\Mslope{\BTTOCM/\xonextwo}
			
			% [NMslope] Calculate the slope of No Mammograms, connecting: (\xtwo, \LAUO) and (\xthree, \IUUO)
			\pgfmathsetmacro\xthreextwo{\xthree - \xtwo}
			\pgfmathsetmacro\LAUONTNM{\IUUO-\LAUO}
			\pgfmathsetmacro\NMslope{\LAUONTNM/\xthreextwo}
			
			% [Mintercept] Calculate the point for Treated at x=0 and x=1 (denoted by l and r)
			\pgfmathsetmacro\Minterceptl{\BTTO-\xone*\Mslope}
			\pgfmathsetmacro\Minterceptr{\Minterceptl + \Mslope}
			
			% [NMintercept] Calculate the point for Untreated at x=0 and x=1 (denoted by l and r)
			\pgfmathsetmacro\NMinterceptl{\LAUO-\xtwo*\NMslope}
			\pgfmathsetmacro\NMinterceptr{\NMinterceptl + \NMslope}
			
			% [Dintercept] Calculate the point for Difference at x=0 and x=1 (denoted by l and r)
			\pgfmathsetmacro\Dinterceptl{\Minterceptl - \NMinterceptl}
			\pgfmathsetmacro\Dinterceptr{\Minterceptr - \NMinterceptr}
			
			% [yvalmax, yvalmin] Calculate Min/Max values of Y to set canvas size
			\pgfmathsetmacro\yvalmax{max(\Minterceptl,\Minterceptr,\NMinterceptl,\NMinterceptr,\Dinterceptl,\Dinterceptr)}
			\pgfmathsetmacro\yvalmin{min(\Minterceptl,\Minterceptr,\NMinterceptl,\NMinterceptr,\Dinterceptl,\Dinterceptr)}
			
			% [yaxismin, yaxismax, yaxislength] Calculate graphing region for the y-axis
			\pgfmathsetmacro\yaxismin{\yvalmin*1.05}
			\pgfmathsetmacro\yaxismax{\yvalmax*1.1}
			\pgfmathsetmacro\yaxislength{\yaxismax-\yaxismin}
			
			% [yaxismin, yaxismax, yaxislength] Calculate graphing region for the y-axis
			\pgfmathsetmacro\pBdi{0.15}
			\pgfmathsetmacro\pIdi{0.41}
			
			\pgfmathsetmacro\MTEintercept{\Minterceptl-\NMinterceptl}
			\pgfmathsetmacro\MTEinterceptr{\Minterceptr-\NMinterceptr}
			
			% MTE(pC) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			\begin{tikzpicture}[>=stealth,element/.style={align=left},square/.style={regular polygon,regular polygon sides=4}]
			% Axis options
			\begin{axis}[					
			width=15cm,					% width of chart
			height=10cm,				% height of chart
			xlabel={$U_D$:  unobserved net cost of treatment},				% x-axis title
			ylabel={\Y},					% y-axis title
			ylabel style={rotate=0},  % rotate the y-axis title
			samples=100,  				% sets the resolution for curved lines
			xmin=0,xmax=1,			    % x-axis range
			ymin=\yaxismin,
			%ymin=1.2*\Dinterceptr,ymax=1.05*\yaxismax,			% y-axis range
			ignore zero=y,			    % ignore the zero on the y-axis (defined in preamble)
			xtick={0,\pB,\pI,1},	    % only show these x ticks
			xticklabels={0,$p_C=\pBdi$, $p_I=\pIdi$,1}, % label x ticks
			ytick={-0.29,0,0.27,0.54,0.55,0.85,1.19,1.35,1.45,1.89},			
			%ytick={0,\LATE,0.64,\IUUO,\LAUO,\LATO,\BTTO},	    % only show these y ticks
			yticklabels={,0,0.27,0.54,,,1.19,1.35,1.45,1.89},
			ymax = \yaxismax,
			legend style={draw=none},	% set legend style, specifically no outline
			legend cell align={left},	% align legend content to the left
			clip=false,					% place nodes outside of plot area
			xticklabel style={
									yshift=-5.5ex,
									name=label,
									append after command={(label.north) edge [solid] ++(0,6ex)},
									/pgf/number format/fixed,
									/pgf/number format/precision=2},
								x label style={at={(axis description cs:0.5,-0.21)},anchor=north}, % ncrease distance between title and x-axis
								yticklabel style={/pgf/number format/fixed},
								% place some extra y ticks ...
								extra y ticks={0},
								% ... but do not repeat the already existent labels
								% (from the "normal" ticks) ...
								extra y tick labels={},
								% ... and set the style of the extra ticks to `major'
								extra tick style={
									grid=major,
								},
			]			
			
			% p* label
		%	\node[label={[align=left]-90:{$p^*=0.48$}},circle,fill,inner sep=2pt,opacity=0] at (axis cs:0.48,-1.15) {};
		%							\draw [thin,black] (0.48,-1.2) -- (0.48,-1);
			
			% mto(p)
			\addplot[
				line width=2.2pt, line cap=round, green, dashed, dash pattern=on 0.6pt off 3.5pt
			] coordinates {(0,\Minterceptl) (1,\Minterceptr)};
			\label{plot:mto}
			
			% muo(p)
			\addplot[
				line width=1.8pt, blue, dashed, dash pattern=on 3.8pt off 2pt
			] coordinates {(0,\NMinterceptl) (1,\NMinterceptr)};
			\label{plot:muo}
			
			% mte(p)
			\addplot[
				line width=2.2pt, line cap=round, red
			] coordinates {(0,\Minterceptl-\NMinterceptl) (1,\Minterceptr-\NMinterceptr)}
			node[pos=0.28,red,pin={[pin edge={black},pin distance=0cm]90:{\textcolor{black}{\small LATE}}}]{};
			\label{plot:mte}
	
			
			
			\node[outer sep=0pt,circle, fill=green,inner sep=2pt] (P) at (0.5*\pB + 0.5*\pI, \LATO) {};
			\node[outer sep=0pt,circle, fill=green,inner sep=2pt] (P) at (0.5*\pB, \BTTO) {};
			\node[outer sep=0pt,diamond, fill=red,inner sep=1.75pt] (P) at (0.5*\pB+0.5*\pI, \LATE) {};
			\node[outer sep=1.3pt,square, fill=blue,inner sep=2pt] (P) at (0.5+0.5*\pI, \IUUO) {};
			\node[outer sep=1.3pt,square, fill=blue,inner sep=2pt] (P) at (0.5*\pB + 0.5*\pI, \LAUO) {};
			
			\node[outer sep=1.3pt, square, draw=blue, fill=white, inner sep=2pt] (P) at (0.075, 1.35) {};
			\node[outer sep=0pt,circle, draw=green, fill=white,inner sep=2pt] (P) at (0.705, 0.55) {};	
			\node[outer sep=0pt,diamond, draw=red, fill=white,inner sep=1.75pt] (P) at (0.075, 0.54) {};
			\node[outer sep=0pt,diamond, draw=red, fill=white,inner sep=1.75pt] (P) at (0.705, -0.29) {};
			
			
			%Dashed Lines OR
			\addplot[
				line width=1pt, black, dashed, opacity=100, thin
			] coordinates {(0.15,\yaxismin) (0.15,\Minterceptl-0.15*\Minterceptl+0.15*\Minterceptr)};
			
			\addplot[
				line width=1pt, black, dashed, opacity=100, thin
			] coordinates {(0.41,\yaxismin) (0.41,\Minterceptl-0.41*\Minterceptl+0.41*\Minterceptr)};

			
			
			% Add right-hand yticklabel
			%\node[anchor=west] () at (axis cs: 1,-0.68) {$-0.68$};
			%\node[anchor=west] () at (axis cs: 1,0.61) {$\phantom{-}0.61$};
			%\node[anchor=west] () at (axis cs: 1,-0.07) {$-0.07$};
			\node[anchor=west] () at (axis cs: 1,-0.29) {$-0.29$};
			\node[anchor=west] () at (axis cs: 1,0.55) {$\phantom{-}0.55$};
			\node[anchor=west] () at (axis cs: 1,0.85) {$\phantom{-}0.85$};
			
			
			% add legend
%			\legend{$\text{MTO}(p) = 2.05 - 2.12p$,MUO($p$),MTE($p$)}
			
			
			% Put Always Takers, Compliers, and Never Takers on the x-axis
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (0,\yaxismin) -- (\pB, \yaxismin) node[pos=0.5, below, yshift = 0pt, align=left] {Always \\ Takers};
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (\pB,\yaxismin) -- (\pI, \yaxismin) node[pos=0.5, below, yshift = 0pt] {Compliers};
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (\pI,\yaxismin) -- (1, \yaxismin) node[pos=0.5, below, yshift = 0pt] {Never Takers};
			
			
			% Legend
			\coordinate (legend) at (axis description cs:1,1);
			
			\end{axis}	
			
		}
	
		
			\matrix [
				draw,
				fill=white,
				,nodes={element},matrix of nodes,
				anchor=north east,
				inner sep=0pt,outer sep=0pt, every node/.style={inner ysep=2pt,inner xsep=1pt},
			] at (legend) {
										   	  & \footnotesize \hspace{-5pt}Intercept   & \footnotesize		S.E.$\quad$			 &  \footnotesize \hspace{-7pt}Slope $$	&\footnotesize \hspace{-8pt} S.E.	$$		   \\ \midrule
		\footnotesize $$ \ref{plot:mto} $ \text{MTO}(p)$ &\footnotesize $2.05^{}$ &\footnotesize $(0.12)\, \quad$ &\footnotesize $\text{-}2.12^{}$ $\quad$ &\footnotesize $(0.75)\quad$    \\
		\footnotesize $$ \ref{plot:muo} $ \text{MUO}(p)$ &\footnotesize $1.41^{}$ &\footnotesize $(0.20)\, \quad$ &\footnotesize $\text{-}0.80^{}$ $\quad$ &\footnotesize $(0.32)\quad$   \\
		\footnotesize $$ \ref{plot:mte} $ \text{MTE}(p)$ &\footnotesize $0.64^{\phantom{}}$  &\footnotesize $(0.24)\, \quad $ &\footnotesize $\text{-}1.32^{\phantom{}}$  $\quad$     &\footnotesize $(0.83)\quad$ \\ \\
					        };
					
					\end{tikzpicture}
					
	\end{document}